<?php

namespace commonsdk;

//define('FFMPEG_PATH', '/usr/local/ffmpeg2/bin/ffmpeg -i "%s" 2>&1');
use FFMpegPush\Command\FFProbeCommand;

define('FFMPEG_PATH', 'C:\dev\ffmpeg\bin\ffmpeg.exe -i "%s" 2>&1');
define('FFMPEG_Cover', 'C:\dev\ffmpeg\bin\ffmpeg.exe -i %s');

class Tools {

    static function getVideoCover($file) {
        $filename = './upload/cover/' . md5($file) . '.jpg';
        if (empty($time))
            $time = '1'; //默认截取第一秒第一帧
        $strlen = strlen($file);
        $str = $file . " -y -f mjpeg -ss 3 -t " . $time . " -s 320x240 " . $filename;
        $command = sprintf(FFMPEG_Cover, $str);
        system($command);
        return $filename;
    }

    static function getVideoInfo($file) {
        $ffprobe = FFProbeCommand::create();
        $data = $ffprobe->format($file);
        return $data;
    }

//用法
//$video_info = getVideoInfo('video.mp4');
//print_r($video_info);


    static public function getUrl($url, $header = false) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回数据不直接输出
        curl_setopt($ch, CURLOPT_ENCODING, "gzip"); //指定gzip压缩
        //add header
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        //add ssl support
        if (substr($url, 0, 5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    //SSL 报错时使用
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);    //SSL 报错时使用
        }
        try {
            $content = curl_exec($ch); //执行并存储结果
        } catch (\Exception $e) {
            $this->_log($e->getMessage());
        }
        $curlError = curl_error($ch);
        if (!empty($curlError)) {
            $this->_log($curlError);
        }
        curl_close($ch);
        return $content;
    }

    static public function httpPost($url, $data) { // 模拟提交数据函数      
        $curl = curl_init(); // 启动一个CURL会话      
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址                  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查      
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在      
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器      
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转      
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer      
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求      
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包      
        curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']); // 读取上面所储存的Cookie信息      
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环      
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容      
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回      
        $tmpInfo = curl_exec($curl); // 执行操作      
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);
        }
        curl_close($curl); // 关键CURL会话      
        return $tmpInfo; // 返回数据      
    }

    /**
     * PHP发送Json对象数据
     *
     * @param $url 请求url
     * @param $jsonStr 发送的json字符串
     * @return array
     */
    static public function http_post_json($url, $jsonStr) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
                )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return array($httpCode, $response);
    }

    //post发送上传文件，数据太大不记日志
    public static function postUploadData($url, $data, $timeout = 30, $headers = array()) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        if (is_array($data)) {
            foreach ($data as $key => $val) {
                curl_setopt($ch, $key, $val);
            }
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

    //构造http multipart/form-data数据包
    static public function buildDouyinUploadData($content) {
        $boundary = '--' . uniqid();
        $data = '--' . $boundary . "\r\n";
        $data .= 'Content-Disposition: form-data; name="video"; filename="a.mp4"' . "\r\n"
                . 'Content-Type:video/mp4' . "\r\n\r\n";
        $data .= $content . "\r\n";
        $data .= '--' . $boundary . "--\r\n";
        return [
            'boundary' => $boundary,
            'data' => $data,
            'len' => strlen($data),
        ];
    }

}
